# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-ldap
_pkgname=lualdap
pkgver=1.2.3
pkgrel=6
pkgdesc="Lua binding to LDAP"
url="https://github.com/bdellegrazie/lualdap"
arch="all"
license="MIT"
depends=""
makedepends="openldap-dev"
checkdepends="openldap openldap-back-bdb"
subpackages=""
source="$_pkgname-$pkgver.tar.gz::https://github.com/bdellegrazie/$_pkgname/archive/v$pkgver.tar.gz
	fix-open_simple-segfault.patch
	fix-anonymous-bind.patch
	fix-search-iter.patch
	script-to-run-test.lua-against-a-dummy-slapd.patch
	update-test.lua-for-5.2.patch"
builddir="$srcdir/$_pkgname-$pkgver"

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done

prepare() {
	default_prepare || return 1

	cd "$builddir"
	sed -i -e '/LUA_VERSION_NUM/d' config

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver" || return 1
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		make CFLAGS="-fPIC -ansi -Wall $(pkg-config --cflags lua$lver)" \
			OPENLDAP_LIB="-lldap -llber" || return 1
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing with Lua $lver..."

		cd "$builddir-$lver"
		make LUA=lua$lver check || return 1
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	replaces="lua$lver-lualdap"  # for backward compatibility (v3.5)
	[ "$lver" = "5.1" ] && replaces="${replaces:-} $pkgname"  # for backward compatibility (<v3.4)

	cd "$builddir-$lver"
	make DESTDIR="$subpkgdir" \
		LUA_LIBDIR="$(pkg-config --variable=INSTALL_CMOD lua$lver)" \
		install

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="51934d18d1ee97a0130545e657497e51542eef97c338df14e7171f94a2d6395fceb00313e1e64e46e6cdeedf85a17c63284595fdf6fbce8b7abce29d28f1b4a7  lualdap-1.2.3.tar.gz
f9f88ef13c8dd13064ca338720d461355cc4d32c03a7e597d7d7a0d9a8a20802bafe0dd479f8c341926fc714fb91489f0649c76675609b84d61ee0327fba8a56  fix-open_simple-segfault.patch
733bbe09edba98f7a549d1e4083a45bf05e1ce9aeffea0416b42e22f1a9f3072a4b36dd56bfde78586edcaafeffcd4880cb72953aeb2952121c92afadd0c8816  fix-anonymous-bind.patch
45170c06fe71bc61565fbdb960d74e59745dc91cbf52c760b8495890d9b02d8fa9361dc4c747d1257db8cb0dac16d97415db6fe69d6d17bb0b37b507aa60627e  fix-search-iter.patch
59622fabc03dd669c554cd8a58a934b5990f482da59bd5b7c243e53a41fbaaa55933ebd345d0d5911c48665fa18f6fcb08e0d9d74a61602f80afe6cb0b20dc4a  script-to-run-test.lua-against-a-dummy-slapd.patch
ffc0f08a141667db402b4b523f0de9893035bdbee5f8e68e48792cd7a29d549931ec4be0715b9f872992f492c1ac2e62c0e05552159571ffa05e5f507a1baf85  update-test.lua-for-5.2.patch"
